package job_guidline_handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"peilian-api/app/global/databases/tables"
	"peilian-api/app/global/variable"
	"peilian-api/utils/format"
	"peilian-api/utils/paginate"
)

func GetOnCampusJob(c *gin.Context) {
	careerId := c.Param("careerId")
	whereStr := "career_id = ?"
	if careerId == "0" {
		whereStr = "career_id <> ?"
	}
	jobs := make([]*tables.SchoolPosition, 0)
	if err := variable.DB.MySQL.Scopes(paginate.Paginate(c.Request)).
		Where(whereStr, careerId).Order("updated_at desc, created_at desc").
		Find(&jobs).Error; err != nil {
		format.NewResponseJson(c).Error(400, err.Error())
		return
	}
	var totalCount int64
	if err := variable.DB.MySQL.Model(&tables.SchoolPosition{}).
		Where(whereStr, careerId).Count(&totalCount).Error; err != nil {
		format.NewResponseJson(c).Error(400, err.Error())
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":  200,
		"data":  jobs,
		"msg":   "ok",
		"total": totalCount,
	})
}
